package com.qyhl.webtv.basiclib.utils.network.Other;

import java.io.File;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by android1 on 2018/1/18.
 */

public class HttpUtil {
    private static final Object lock = new Object();
    /**
     * 超时时间
     */
    private static final int TIMEOUT = 20;

    private static ApiCommon request;


    public static ApiCommon getCommonInstance() {
        if (request == null) {
            synchronized (lock) {
                if (request == null) {
                    OkHttpClient client = new OkHttpClient.Builder()
                            .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                            .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                            .build();

                    Retrofit retrofit = new Retrofit.Builder()
                            .client(client)
                            .baseUrl("http://192.168.10.117:8080/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .build();
                    request = retrofit.create(ApiCommon.class);
                }
            }
        }
        return request;
    }





    public static <T> void enqueue(Observable<BaseBean<T>> observable, HttpCallback<T> httpCallback) {
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(httpCallback);
    }

    public static void enqueuedown(Observable<ResponseBody> observable, File file, HttpDownCallback httpCallback) {
            httpCallback.setFile(file);
            observable.subscribeOn(Schedulers.io())
                    .subscribe(httpCallback);
    }
}
